//
//  MZengJiaVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/14.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "MZengJiaVC.h"

#import "HouseCKCell.h"
#import "CCZTableButton.h"
#import "KSDatePicker.h"
#import "TodoHttpManager.h"
#import "ManagerHuiVC.h"
@interface MZengJiaVC ()<UITextFieldDelegate>
{
    NSString *wuTStr;
    NSString *HousID;
    NSString *houseType;
    NSString *wuType;
}
@property (nonatomic, strong) CCZTableButton *table;

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)UILabel *dataLab;
@property(nonatomic,strong)UIButton *wuYBtn;
@property(nonatomic,strong)UITextField *louTF;
@property(nonatomic,strong)UITextField *unitTF;
@property(nonatomic,strong)UITextField *houseTF;
@property(nonatomic,strong)UIButton *lookBtn;
@property(nonatomic,strong)UIView *oneView;
@property(nonatomic,strong)UIView *twoView;
@property(nonatomic,strong)NSMutableArray *laArr;
@property(nonatomic,strong)NSMutableArray *laEVArr;
@property(nonatomic,strong)NSMutableDictionary *chaDic;





@end

@implementation MZengJiaVC


-(void)viewWillAppear:(BOOL)animated
{
    //物业类型
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDYongJinWuYeType:) name:TDYongJinWuYeType object:nil];
    [_manager userYongJinWuYeType];
    //特别优惠查询
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerYouHuiChaXun:) name:TDManagerYouHuiChaXun object:nil];
    
    //特别优惠保存
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerYouHuiBaoCun:) name:TDManagerYouHuiBaoCun object:nil];
    [self setUpLotOf];
}

//物业类型

-(void)didTDYongJinWuYeType:(NSNotification *)notifica
{
    if (notifica.object != 0)
    {
        _laArr = [notifica.object objectForKey:@"data"];
        for (int i = 0; i<_laArr.count; i++)
        {
            NSDictionary *dic = _laArr[i];
        
            [_laEVArr addObject:dic[@"name"]];
       
            
           [self setUPLook];
        }
    }
}
 //特别优惠查询
-(void)didTDManagerYouHuiChaXun:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int statue = [[notifi.object objectForKey:@"status"] intValue];
        if (statue == 1)
        {
            _chaDic = [notifi.object objectForKey:@"data"];
            NSLog(@"-------查询的有什么--------%@",_chaDic);
           
            HousID = [_chaDic objectForKey:@"id"];
            houseType = [_chaDic objectForKey:@"type"];
           // [self setUpLotOf];
            
                [self setUpLotOf];
               
        
        }
      else
      {
          NSString *infoStr= [notifi.object objectForKey:@"info"];
          [_manager MakeMyselfAlert:infoStr andIsNeedTitle:NO andShowTime:2.0];
            _kNameLab.text = @"";
          _phoneLab.text = @"";
          _areaLab.text = @"";
          _biaoLab.text = @"";
          _zongLab.text = @"";
          _renLab.text = @"";
          _anYLab.text = @"";
          _xieLab.text = @"";
          _danLab.text = @"";
      }
    }
}

//特别优惠保存
-(void)didTDManagerYouHuiBaoCun:(NSNotification *)noyi
{
    if (noyi.object != 0)
    {
        int SS = [[noyi.object objectForKey:@"status"] intValue];
        if (SS == 1)
        {
             ManagerHuiVC *youVC = [[ManagerHuiVC alloc] init];
            [self.navigationController pushViewController:youVC animated:NO];
        }
        else
        {
            NSString *xinStr = [noyi.object objectForKey:@"info"];
            [_manager MakeMyselfAlert:xinStr andIsNeedTitle:NO andShowTime:2.0];
        }
        
        
        
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerYouHuiChaXun object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerYouHuiBaoCun object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDYongJinWuYeType object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _manager = [TodoHttpManager sharedManager];

    _laEVArr = [NSMutableArray array];
    _laArr = [NSMutableArray array];
    _chaDic = [NSMutableDictionary dictionary];
    self.title = @"优惠申请";
    self.view.backgroundColor = BgColor;


    
    [self setUPLook];
   // [self setUpLotOf];

}

-(void)setUPLook
{
    
    _oneView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, B_WIDTH, 120)];
    _oneView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_oneView];
    UILabel *oneLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 60, 18)];
    oneLab.text = @"申请日期:";

    oneLab.font = [UIFont systemFontOfSize:12];
    oneLab.textColor = [UIColor blackColor];
    [_oneView addSubview:oneLab];
    
    _dataLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 6, 180, 22)];
    _dataLab.font = [UIFont systemFontOfSize:12];
    _dataLab.layer.cornerRadius =5;
    _dataLab.layer.borderWidth = 0.5;
    _dataLab.textAlignment = NSTextAlignmentCenter;
    _dataLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_oneView addSubview:_dataLab];
    
    UIButton *riLBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    riLBtn.frame = CGRectMake(235, 8, 20, 18);
    [riLBtn setBackgroundImage:[UIImage imageNamed:@"btnRiLi"] forState:0];
    [riLBtn addTarget:self action:@selector(riLiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_oneView addSubview:riLBtn];

    UILabel *oneLin = [[UILabel alloc] initWithFrame:CGRectMake(0, 32, B_WIDTH, 0.5)];
    oneLin.backgroundColor = [UIColor grayColor];
    [_oneView addSubview:oneLin];
    

    //物业类型
    UILabel *zuoLab = [[UILabel alloc] init];
    zuoLab.frame = CGRectMake(10, 40, 60, 20);
    zuoLab.text = @"物业类型:";
   // zuoLab.textAlignment = NSTextAlignmentRight;
    zuoLab.textColor = [UIColor blackColor];
    zuoLab.font = [UIFont systemFontOfSize:12];
    [_oneView addSubview:zuoLab];
    
    _wuYBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _wuYBtn.frame = CGRectMake(80, 38, 180, 22);
    _wuYBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _wuYBtn.layer.cornerRadius =5;
    _wuYBtn.layer.borderWidth = 0.5;
    [_wuYBtn setTitle:@"全部" forState:0];
    [_wuYBtn setTitleColor:[UIColor blackColor] forState:0];
    [_wuYBtn addTarget:self action:@selector(wuYBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _wuYBtn.layer.borderColor = [UIColor grayColor].CGColor;
    [_oneView addSubview:_wuYBtn];
    
    UIImageView *imgV1 = [[UIImageView alloc] initWithFrame:CGRectMake(245, 45, 10, 10)];
    imgV1.image = [UIImage imageNamed:@"sanJiao"];
    [_oneView addSubview:imgV1];
    
    self.table = [[CCZTableButton alloc] initWithFrame:CGRectMake(80, 124, 180, 0)];
    self.table.offsetXOfArrow = -30;
    [self.table addItems:_laEVArr exceptItem:@"全部"];
    [self.table selectedAtIndexHandle:^(NSUInteger index, NSString *itemName) {
        
        [_wuYBtn setTitle:itemName forState:UIControlStateNormal];
        
        wuTStr = itemName;
    
        
    }];
    
    UILabel *twoLin = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, B_WIDTH, 0.5)];
    twoLin.backgroundColor = [UIColor grayColor];
    [_oneView addSubview:twoLin];
    UILabel *zhuLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 71, 100, 18)];
    zhuLab.text = @"主售房源位置";
    zhuLab.textColor = [UIColor blackColor];
    zhuLab.font = [UIFont systemFontOfSize:12];
    [_oneView addSubview:zhuLab];
    
    _louTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 95, 50, 22)];
    _louTF.borderStyle = UITextBorderStyleRoundedRect;
    //设置输入框的背景颜色
    _louTF.delegate = self;
    _louTF.backgroundColor = [UIColor whiteColor];
    _louTF.font = [UIFont systemFontOfSize:14];
    _louTF.textColor = [UIColor blackColor];
    [_oneView addSubview:_louTF];

    UILabel *haoLab =[[UILabel alloc] initWithFrame:CGRectMake(60, 97, 30, 18)];
    haoLab.textColor = [UIColor blackColor];
    haoLab.text = @"号楼";
    haoLab.font = [UIFont systemFontOfSize:12];
    [_oneView addSubview:haoLab];
    
    _unitTF = [[UITextField alloc] initWithFrame:CGRectMake(90, 95, 50, 22)];
    _unitTF.borderStyle = UITextBorderStyleRoundedRect;
    //设置输入框的背景颜色
    _unitTF.delegate = self;
    _unitTF.backgroundColor = [UIColor whiteColor];
    _unitTF.font = [UIFont systemFontOfSize:14];
    _unitTF.textColor = [UIColor blackColor];
    [_oneView addSubview:_unitTF];

    UILabel *danLab = [[UILabel alloc] initWithFrame:CGRectMake(142, 97, 30, 18)];
    danLab.textColor = [UIColor blackColor];
    danLab.text = @"单元";
    danLab.font = [UIFont systemFontOfSize:12];
    [_oneView addSubview:danLab];
    
    _houseTF = [[UITextField alloc] initWithFrame:CGRectMake(172, 95, 50, 22)];
    _houseTF.borderStyle = UITextBorderStyleRoundedRect;
    //设置输入框的背景颜色
    _houseTF.delegate = self;
    _houseTF.backgroundColor = [UIColor whiteColor];
    _houseTF.font = [UIFont systemFontOfSize:14];
    _houseTF.textColor = [UIColor blackColor];
    [_oneView addSubview:_houseTF];
    

    UILabel *fangLab =  [[UILabel alloc] initWithFrame:CGRectMake(222, 97, 30, 18)];
    fangLab.textColor = [UIColor blackColor];
    fangLab.text = @"号房";
    fangLab.font = [UIFont systemFontOfSize:12];
    [_oneView addSubview:fangLab];
    
   
    _lookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _lookBtn.frame = CGRectMake(260, 95, 60, 22);
    _lookBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _lookBtn.layer.cornerRadius =5;
    _lookBtn.backgroundColor = [UIColor orangeColor];
    [_lookBtn setTitle:@"查看" forState:0];
    [_lookBtn setTitleColor:[UIColor whiteColor] forState:0];
    [_lookBtn addTarget:self action:@selector(lookBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_oneView addSubview:_lookBtn];

}

-(void)setUpLotOf
{
    _twoView = [[UIView alloc] initWithFrame:CGRectMake(0, 185, B_WIDTH, 320)];
    _twoView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_twoView];
    for (int a= 0; a<9; a++)
    {
        UILabel *lineLab = [[UILabel alloc] init];
        lineLab.frame = CGRectMake(0, 32+32*a, B_WIDTH, 0.5);
        lineLab.backgroundColor = [UIColor grayColor];
        [_twoView addSubview:lineLab];
    }
    
    NSArray *titArr= @[@"客户姓名:",@"联系电话:",@"销售面积:",@"销售表价:",@"表价总价:",@"认筹优惠:",@"案场优惠:",@"协议总价:",@"协议单价:",@"备注信息:"];
    for (int b = 0; b<10; b++)
    {
        UILabel *titLab = [[UILabel alloc] init];
        titLab.frame = CGRectMake(10, 7+32*b, 60, 18);
        titLab.text = titArr[b];
        titLab.textColor = [UIColor blackColor];
        titLab.font = [UIFont systemFontOfSize:12];
        [_twoView addSubview:titLab];
    }
    
    _kNameLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 180, 22)];
    _kNameLab.font = [UIFont systemFontOfSize:12];
    _kNameLab.layer.cornerRadius =5;
    _kNameLab.layer.borderWidth = 0.5;
    _kNameLab.text = _chaDic[@"name"];
    _kNameLab.textAlignment = NSTextAlignmentCenter;
    _kNameLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_kNameLab];
    

    _phoneLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 37, 180, 22)];
    _phoneLab.font = [UIFont systemFontOfSize:12];
    _phoneLab.layer.cornerRadius =5;
    _phoneLab.layer.borderWidth = 0.5;
     _phoneLab.text = _chaDic[@"phone"];
    _phoneLab.textAlignment = NSTextAlignmentCenter;
    _phoneLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_phoneLab];
    
    _areaLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 69, 180, 22)];
    _areaLab.font = [UIFont systemFontOfSize:12];
    _areaLab.layer.cornerRadius =5;
    _areaLab.layer.borderWidth = 0.5;
     _areaLab.text = _chaDic[@"area"];
    _areaLab.textAlignment = NSTextAlignmentCenter;
    _areaLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_areaLab];

    _biaoLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 101, 180, 22)];
    _biaoLab.font = [UIFont systemFontOfSize:12];
    _biaoLab.layer.cornerRadius =5;
    _biaoLab.layer.borderWidth = 0.5;
     _biaoLab.text = _chaDic[@"price"];
    _biaoLab.textAlignment = NSTextAlignmentCenter;
    _biaoLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_biaoLab];
    
    _zongLab = [[UILabel alloc] initWithFrame:CGRectMake(80, 133, 180, 22)];
    _zongLab.font = [UIFont systemFontOfSize:12];
    _zongLab.layer.cornerRadius =5;
    _zongLab.layer.borderWidth = 0.5;
     _zongLab.text = _chaDic[@"price_all"];
    _zongLab.textAlignment = NSTextAlignmentCenter;
    _zongLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_zongLab];

    _renLab =  [[UILabel alloc] initWithFrame:CGRectMake(80, 165, 180, 22)];
    _renLab.font = [UIFont systemFontOfSize:12];
    _renLab.layer.cornerRadius =5;
    _renLab.layer.borderWidth = 0.5;
     _renLab.text = _chaDic[@"renchou_yh"];
    _renLab.textAlignment = NSTextAlignmentCenter;
    _renLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_renLab];
    
    _anYLab =  [[UILabel alloc] initWithFrame:CGRectMake(80, 197, 180, 22)];
    _anYLab.font = [UIFont systemFontOfSize:12];
    _anYLab.layer.cornerRadius =5;
    _anYLab.layer.borderWidth = 0.5;
     _anYLab.text = _chaDic[@"acyh"];
    _anYLab.textAlignment = NSTextAlignmentCenter;
    _anYLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_anYLab];
    
     _xieLab=  [[UILabel alloc] initWithFrame:CGRectMake(80, 229, 180, 22)];
    _xieLab.font = [UIFont systemFontOfSize:12];
    _xieLab.layer.cornerRadius =5;
    _xieLab.layer.borderWidth = 0.5;
     _xieLab.text = _chaDic[@"xieyi_all"];
    _xieLab.textAlignment = NSTextAlignmentCenter;
    _xieLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_xieLab];
    
    _danLab =  [[UILabel alloc] initWithFrame:CGRectMake(80, 261, 180, 22)];
    _danLab.font = [UIFont systemFontOfSize:12];
    _danLab.layer.cornerRadius =5;
    _danLab.layer.borderWidth = 0.5;
     _danLab.text = _chaDic[@"xieyi_dan"];
    _danLab.textAlignment = NSTextAlignmentCenter;
    _danLab.layer.borderColor = [UIColor grayColor].CGColor;
    [_twoView addSubview:_danLab];
    
    _beiTF =  [[UITextField alloc] initWithFrame:CGRectMake(80, 293, 180, 22)];
    _beiTF.borderStyle = UITextBorderStyleRoundedRect;
    _beiTF.placeholder = @"请输入备注信息";
    //设置输入框的背景颜色
    _beiTF.delegate = self;
    _beiTF.backgroundColor = [UIColor whiteColor];
    _beiTF.font = [UIFont systemFontOfSize:14];
    _beiTF.textColor = [UIColor blackColor];
    [_twoView addSubview:_beiTF];
    
    _faBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _faBtn.frame = CGRectMake(10, 510, B_WIDTH-10*2, 35);
    _faBtn.backgroundColor =[UIColor orangeColor];
    _faBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _faBtn.layer.cornerRadius =5;
    [_faBtn setTitle:@"发送审批" forState:0];
    [_faBtn setTitleColor:[UIColor whiteColor] forState:0];
    [_faBtn addTarget:self action:@selector(shenPiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _faBtn.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:_faBtn];
    

    
    
    
}






#pragma mark 物业类型下拉
-(void)wuYBtnClick:(UIButton *)btn
{
    [self.table show];
}

#pragma mark 日历的点击事件
-(void)riLiBtnClick:(UIButton *)sender
{
    //NSLog(@"日历的 tag 值====== %ld",sender.tag);
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH - 40, 300)];
    picker.appearance.radius = 5;
    
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            
            _dataLab.text = [formatter stringFromDate:currentDate];
          
        
        }
    };
    // 显示
    [picker show];
    
    
}
#pragma mark 查看点击事件
-(void)lookBtnClick:(UIButton *)btn
{
    //TODO:查询
    [_manager userManagerYouHuiChaXunWithWuType:wuTStr withLouHao:_louTF.text withUnit:_unitTF.text withQuYu:nil withRoom:_houseTF.text];
}


#pragma mark  发送审批
-(void)shenPiBtnClick:(UIButton *)btn
{
    //TODO:发送审批
     NSLog(@"日历是多少=======%@",_dataLab.text);
    [_manager userManageryouHuiBaoCunWithID:HousID withType:houseType withBeiZhu:_beiTF.text withDate: _dataLab.text withWuYe:wuTStr];
    NSLog(@"-------111111-------%@",HousID);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
